package com.usian.service;

import com.usian.mapper.TbContentCategoryMapper;
import com.usian.pojo.TbContentCategory;
import com.usian.pojo.TbContentCategoryExample;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * @author shkstart
 * @create 2021-01-07 15:31
 */
@Service
public class ContentCategoryServiceImpl implements ContentCategoryService{
    @Autowired(required = false)
    private TbContentCategoryMapper tbContentCategoryMapper;

    @Override
    public List<TbContentCategory> selectContentCategoryByParentId(Long parentId){
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(parentId);
        List<TbContentCategory> list = this.tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        return list;
    }

    @Override
    public Integer insertContentCategory(TbContentCategory tbContentCategory){
        //1、添加内容分类
        tbContentCategory.setUpdated(new Date());
        tbContentCategory.setCreated(new Date());
        tbContentCategory.setIsParent(false);
        tbContentCategory.setSortOrder(1);
        tbContentCategory.setStatus(1);
        int contentCategoryNum = this.tbContentCategoryMapper.insert(tbContentCategory);

        TbContentCategory contentCategory = this.tbContentCategoryMapper.selectByPrimaryKey(tbContentCategory.getParentId());

        if (!contentCategory.getIsParent()){
            contentCategory.setIsParent(true);
            contentCategory.setUpdated(new Date());
            this.tbContentCategoryMapper.updateByPrimaryKey(contentCategory);
        }
        return contentCategoryNum;
    }

    @Override
    public Integer deleteContentCategoryById(Long categoryId){
        TbContentCategory tbContentCategory = this.tbContentCategoryMapper.selectByPrimaryKey(categoryId);

        //父节点，不允许删除
        if (tbContentCategory.getIsParent()==true){
            return 0;
        }
        //不是父节点
        tbContentCategoryMapper.deleteByPrimaryKey(categoryId);

        //当前节点的兄弟节点
        TbContentCategoryExample tbContentCategoryExample = new TbContentCategoryExample();
        TbContentCategoryExample.Criteria criteria = tbContentCategoryExample.createCriteria();
        criteria.andParentIdEqualTo(tbContentCategory.getParentId());
        List<TbContentCategory> tbContentCategoryList = tbContentCategoryMapper.selectByExample(tbContentCategoryExample);
        //删除之后如果父节点没有孩子，则修改isParent为false
        if (tbContentCategoryList.size()>0){
            TbContentCategory parenttbContentCategroy = new TbContentCategory();
            parenttbContentCategroy.setId(tbContentCategory.getParentId());
            parenttbContentCategroy.setIsParent(false);
            parenttbContentCategroy.setUpdated(new Date());
            this.tbContentCategoryMapper.updateByPrimaryKeySelective(parenttbContentCategroy);
        }
        return 200;
    }

    @Override
    public Integer updateContentCategory(Long id, String name) {
        TbContentCategory tbContentCategory = this.tbContentCategoryMapper.selectByPrimaryKey(id);
        tbContentCategory.setName(name);
        return tbContentCategoryMapper.updateByPrimaryKeySelective(tbContentCategory);
    }

}
